#!/bin/bash

# DCEVM热更新测试脚本

echo "=================================="
echo "  DCEVM热更新功能测试"
echo "=================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查Tomcat是否运行
if ! pgrep -f "catalina" > /dev/null; then
    echo -e "${RED}错误：Tomcat未运行，请先启动Tomcat${NC}"
    echo "运行命令：./start-tomcat.sh"
    exit 1
fi

echo -e "${GREEN}✓ Tomcat正在运行${NC}"

# 检查DCEVM版本
echo -e "${BLUE}检查DCEVM版本...${NC}"
DCEVM_VERSION=$("/Library/Java/JavaVirtualMachines/dcevm8u282.jdk/Contents/Home/bin/java" -version 2>&1 | grep "Dynamic Code Evolution")
if [[ $DCEVM_VERSION == *"Dynamic Code Evolution"* ]]; then
    echo -e "${GREEN}✓ DCEVM已启用：$DCEVM_VERSION${NC}"
else
    echo -e "${RED}✗ DCEVM未启用${NC}"
    exit 1
fi

# 备份原始控制器文件
echo -e "${BLUE}备份原始控制器文件...${NC}"
cp src/com/wordcounter/controller/WordCounterController.java src/com/wordcounter/controller/WordCounterController.java.backup

# 测试1：修改返回消息
echo -e "${BLUE}测试1：修改控制器返回消息...${NC}"
sed -i '' 's/return "result";/return "result"; \/\/ 热更新测试：'$(date +%H:%M:%S)'/' src/com/wordcounter/controller/WordCounterController.java

# 重新编译
echo -e "${BLUE}重新编译...${NC}"
./build.sh

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 编译成功${NC}"
else
    echo -e "${RED}✗ 编译失败${NC}"
    exit 1
fi

# 等待几秒钟让类加载器重新加载
echo -e "${BLUE}等待类重新加载...${NC}"
sleep 5

# 测试应用是否正常工作
echo -e "${BLUE}测试应用功能...${NC}"
RESPONSE=$(curl -s -X POST -d "content=热更新测试" http://localhost:8080/wordcounter/count)

if [[ $RESPONSE == *"热更新测试"* ]]; then
    echo -e "${GREEN}✓ 热更新测试成功！应用正常运行${NC}"
else
    echo -e "${YELLOW}⚠ 热更新可能未生效，但应用仍在运行${NC}"
fi

# 测试2：修改方法逻辑
echo -e "${BLUE}测试2：修改方法逻辑...${NC}"
# 在countWords方法中添加一个注释
sed -i '' 's/int wordCount = content.trim().isEmpty() ? 0 : content.trim().split("\\\\s+").length;/int wordCount = content.trim().isEmpty() ? 0 : content.trim().split("\\\\s+").length; \/\/ 热更新测试2：'$(date +%H:%M:%S)'/' src/com/wordcounter/controller/WordCounterController.java

# 重新编译
echo -e "${BLUE}重新编译...${NC}"
./build.sh

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 编译成功${NC}"
else
    echo -e "${RED}✗ 编译失败${NC}"
fi

# 等待类重新加载
echo -e "${BLUE}等待类重新加载...${NC}"
sleep 5

# 测试应用
echo -e "${BLUE}测试应用功能...${NC}"
RESPONSE2=$(curl -s -X POST -d "content=热更新测试2" http://localhost:8080/wordcounter/count)

if [[ $RESPONSE2 == *"热更新测试2"* ]]; then
    echo -e "${GREEN}✓ 热更新测试2成功！${NC}"
else
    echo -e "${YELLOW}⚠ 热更新测试2可能未生效${NC}"
fi

# 恢复原始文件
echo -e "${BLUE}恢复原始文件...${NC}"
mv src/com/wordcounter/controller/WordCounterController.java.backup src/com/wordcounter/controller/WordCounterController.java
./build.sh

echo -e "${GREEN}=================================="
echo "热更新测试完成！"
echo "=================================="
echo ""
echo -e "${BLUE}说明：${NC}"
echo "1. 如果看到'✓ 热更新测试成功'，说明DCEVM热更新功能正常工作"
echo "2. 如果看到'⚠ 热更新可能未生效'，可能需要重启Tomcat"
echo "3. 热更新主要适用于方法体内部的修改，不适用于："
echo "   - 添加/删除方法"
echo "   - 修改方法签名"
echo "   - 修改类结构"
echo ""
echo -e "${YELLOW}注意：${NC}热更新功能需要DCEVM支持，普通JDK不支持此功能" 